using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using outlook = Microsoft.Office.Interop.Outlook;
using interop = System.Runtime.InteropServices;

namespace ESnatch
{
    public partial class SetOutputForm : Form
    {

        string[] TranslatableTypes = OutputMapping.getTranslatableTypes();
        string[] DefaultOutputTypes = OutputMapping.getDefaultOutputTypes();

        List<OutputMapping> m_mappings;

        string m_savePath = null;
        outlook.MAPIFolder m_folder = null;
        public SetOutputForm(string savePath, outlook.MAPIFolder folder, List<OutputMapping> mappings)
        {
            InitializeComponent();
            m_savePath = savePath;
            m_folder = folder;
            m_mappings = mappings;
        }


        public List<OutputMapping> Mappings
        {
            get
            {
                return m_mappings;
            }
        }

        private string MapDefaults(string srcType)
        {
            for (int ix = 0; ix < TranslatableTypes.Length; ix++)
            {
                if (srcType.CompareTo(TranslatableTypes[ix]) == 0)
                    return DefaultOutputTypes[ix];
            }
            return "Text";
        }

        private void InitMappings()
        {
            m_mappings = new List<OutputMapping>();
            Type t = typeof(outlook.MailItemClass);

            PropertyInfo[] pinfoArray = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            
            foreach (PropertyInfo pinfo in pinfoArray)
            {
                OutputMapping mapping = new OutputMapping(pinfo);
                mapping.TargetType = MapDefaults(pinfo.PropertyType.Name);
                m_mappings.Add(mapping);
            } 
        }

        private void SetOuputForm_Load(object sender, EventArgs e)
        {
            tbSaveAs.Text = m_savePath;

            Type t = typeof(outlook.MailItemClass);
            if (m_mappings == null)
                InitMappings();

            lvAvailProperties.Items.Clear();
            foreach (OutputMapping mapping in m_mappings)
            {
                string[] subItems = 
                    { mapping.SrcPropInfo.Name, mapping.SrcPropInfo.PropertyType.Name, 
                        mapping.TargetType, mapping.IncludeTxt };
                string targType = subItems[2];                
                ListViewItem lvi = new ListViewItem (subItems);
                lvi.UseItemStyleForSubItems = true;
                if (!mapping.IsMappable)
                    lvi.ForeColor = Color.LightGray;
                lvi.Tag = mapping;
                lvAvailProperties.Items.Add(lvi);
            }                
        }

        private void mnuItemToggleInclude_Click(object sender, EventArgs e)
        {
            if (lvAvailProperties.SelectedItems.Count < 1)
            {                
                return;
            }
            ListViewItem lvi = lvAvailProperties.SelectedItems[0];
            OutputMapping tag = (OutputMapping)lvi.Tag;
            if (tag.IsMappable)
            {
                tag.Include = (!tag.Include);
                lvi.SubItems[3].Text = tag.IncludeTxt;
                ToolStripMenuItem ti0 = (ToolStripMenuItem)contextMenuStripLvProps.Items[0];
                ti0.Checked = tag.Include;
            }
           
        }

        private void contextMenuStripLvProps_Opening(object sender, CancelEventArgs e)
        {
            if (lvAvailProperties.SelectedItems.Count < 1)
            {
                e.Cancel = true;
                return;
            }
            ListViewItem lvi = lvAvailProperties.SelectedItems[0];
            OutputMapping tag = (OutputMapping)lvi.Tag;
            if(tag.IsMappable)
            {
                ToolStripMenuItem ti0 = (ToolStripMenuItem)contextMenuStripLvProps.Items[0];
                ti0.Checked = tag.Include;
            }
            else
                e.Cancel = true;
        }

        private void mnuItemChangeTargetType_Click(object sender, EventArgs e)
        {
            if (lvAvailProperties.SelectedItems.Count < 1)
            {
                return;
            }
            ListViewItem lvi = lvAvailProperties.SelectedItems[0];
            OutputMapping tag = (OutputMapping)lvi.Tag;
            if (tag.IsMappable)
            {                
                SelectOutputType dlg = new SelectOutputType(tag.TargetType, tag.SrcPropInfo.Name, tag.SrcPropInfo.PropertyType.Name);
                DialogResult dr = dlg.ShowDialog();
                if (dr == DialogResult.OK)
                {             
                    
                    dr = MessageBox.Show("Do you wish to apply this change to all properties of type " + tag.SrcPropInfo.PropertyType.Name + "?", "Email Snatch", MessageBoxButtons.YesNo);
                    if (dr == DialogResult.Yes)
                    {
                        string srcType = tag.SrcPropInfo.PropertyType.Name;
                        foreach (ListViewItem lvi2 in lvAvailProperties.Items)
                        {
                            OutputMapping tag2 = (OutputMapping)lvi2.Tag;
                            if (tag2.IsMappable)
                            {
                                string srcType2 = tag2.SrcPropInfo.PropertyType.Name;
                                if (srcType2.CompareTo(srcType) == 0)
                                {
                                    tag2.TargetType = dlg.SelectedType;
                                    lvi2.SubItems[2].Text = tag2.TargetType;
                                }
                            }
                        }
                    }
                    else
                    {
                        tag.TargetType = dlg.SelectedType;
                        lvi.SubItems[2].Text = tag.TargetType;
                    }
                }               
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }
        
    }
}